home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / glchess / uci.py < prev   
Encoding:
Python Source  |  2009-09-22  |  5.2 KB  |  186 lines

  1. # -*- coding: utf-8 -*-
  2.  
  3. class StateMachine:
  4.     """
  5.     """
  6.     
  7.     STATE_IDLE = 'IDLE'
  8.     STATE_CONNECTING = 'CONNECTING'
  9.     
  10.     def __init__(self):
  11.         """
  12.         """
  13.         self.options = {}
  14.         self.__queuedCommands = []
  15.         self.buffer = ''
  16.         self.__haveMoves = False
  17.         self.__readyToConfigure = False    
  18.         self.__ready            = False
  19.         self.__inCallback       = False
  20.         self.__queuedCommands   = None
  21.         self.__positionCommand = 'position startpos'
  22.         
  23.     def logText(self, text, style):
  24.         """
  25.         """
  26.         pass
  27.         
  28.     def onOutgoingData(self, data):
  29.         """
  30.         """
  31.         pass
  32.  
  33.     def onMove(self, move):
  34.         """Called when the AI makes a move.
  35.         
  36.         'move' is the move the AI has decided to make (string).
  37.         """
  38.         print 'UCI move: ' + move
  39.         
  40.     def registerIncomingData(self, data):
  41.         """
  42.         """
  43.         self.__inCallback = True
  44.         self.buffer += data
  45.         while True:
  46.             index = self.buffer.find('\n')
  47.             if index < 0:
  48.                 break
  49.             line = self.buffer[:index]
  50.             self.buffer = self.buffer[index + 1:]
  51.             self.parseLine(line)
  52.         self.__inCallback = False
  53.         
  54.         if self.__options is not None and self.__readyToConfigure:
  55.             options = self.__options
  56.             self.__options = None
  57.             self.configure(options)
  58.  
  59.         # Send queued commands once have OK
  60.         if len(self.__queuedCommands) > 0 and self.__ready:
  61.             commands = self.__queuedCommands
  62.             self.__queuedCommands = []
  63.             for c in commands:
  64.                 self.__sendCommand(c)
  65.                 
  66.     def __sendCommand(self, command):
  67.         """
  68.         """
  69.         if self.__ready and not self.__inCallback:
  70.             self.onOutgoingData(command + '\n')
  71.         else:
  72.             self.__queuedCommands.append(command)
  73.  
  74.     def start(self):
  75.         """
  76.         """
  77.         self.onOutgoingData('uci\n')
  78.         
  79.     def startGame(self):
  80.         """
  81.         """
  82.         self.__sendCommand('ucinewgame')
  83.         self.__sendCommand(self.__positionCommand)
  84.  
  85.     def configure(self, options = []):
  86.         """
  87.         """
  88.         if not self.__readyToConfigure:
  89.             self.__options = options
  90.             return
  91.  
  92.         for option in options:
  93.             if not hasattr(option, 'name'):
  94.                 print 'Ignoring unnamed UCI option'
  95.                 continue
  96.             if option.value == '':
  97.                 continue
  98.             self.onOutgoingData('setoption ' + option.name + ' value ' + option.value + '\n')
  99.         self.onOutgoingData('isready\n')
  100.  
  101.     def requestMove(self, whiteTime, blackTime, ownTime):
  102.         """
  103.         """
  104.         # Some AI's don't work unless assigned some time
  105.         # TODO: which ones? I think Glaurung had issues
  106.         if whiteTime == 0:
  107.             whiteTime = 30000
  108.         if blackTime == 0:
  109.             blackTime = 30000
  110.             
  111.         self.__sendCommand('go wtime %d btime %d' % (whiteTime, blackTime))
  112.         
  113.     def undoMove(self):
  114.         """
  115.         """
  116.         self.__sendCommand('stop');
  117.         (self.__positionCommand, _) = self.__positionCommand.rsplit(" ", 1)
  118.         if self.__positionCommand.endswith(' moves'):
  119.             self.__haveMoves = False
  120.             self.__positionCommand = 'position startpos'
  121.         self.__sendCommand(self.__positionCommand)        
  122.  
  123.     def reportMove(self, move, isSelf):
  124.         """
  125.         """
  126.         if not self.__haveMoves:
  127.             self.__positionCommand += ' moves'
  128.         self.__haveMoves = True
  129.         self.__positionCommand += ' ' + move
  130.         self.__sendCommand(self.__positionCommand)
  131.  
  132.     def parseLine(self, line):
  133.         """
  134.         """
  135.         words = line.split()
  136.         
  137.         while True:
  138.             if len(words) == 0:
  139.                 self.logText(line + '\n', 'input')
  140.                 return
  141.             
  142.             style = self.parseCommand(words[0], words[1:])
  143.             if style is not None:
  144.                 self.logText(line + '\n', style)
  145.                 return
  146.  
  147.             print 'WARNING: Unknown command: ' + repr(words[0])
  148.             words = words[1:]
  149.  
  150.     def parseCommand(self, command, args):
  151.         """
  152.         """
  153.         if command == 'id':
  154.             return 'info'
  155.         
  156.         elif command == 'uciok':
  157.             if len(args) != 0:
  158.                 print 'WARNING: Arguments on uciok: ' + str(args)
  159.             self.__readyToConfigure = True
  160.             return 'info'
  161.         
  162.         elif command == 'readyok':
  163.             if len(args) != 0:
  164.                 print 'WARNING: Arguments on readyok: ' + str(args)
  165.             self.__ready = True
  166.             return 'info'
  167.         
  168.         elif command == 'bestmove':
  169.             if len(args) == 0:
  170.                 print 'WARNING: No move with bestmove'
  171.                 return 'error'
  172.             else:
  173.                 move = args[0]
  174.                 self.onMove(move)
  175.                 
  176.                 # TODO: Check for additional ponder information
  177.                 return 'move'
  178.         
  179.         elif command == 'info':
  180.             return 'info'
  181.         
  182.         elif command == 'option':
  183.             return 'info'
  184.         
  185.         return None
  186.